/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BuscarElector.java
 *
 * Created on 14-ene-2013, 7:12:47
 */
package sisvoto.vista;

import java.awt.event.KeyEvent;
import java.util.ArrayList;
import net.sf.jasperreports.engine.JRException;
import sisvoto.DAO.ConsultasBD;
import sisvoto.Reportes.Reportes;
import sisvoto.Utiles.Mensajes;
import sisvoto.Utiles.Util;
import sisvoto.modelo.Elector;

/**
 *
 * @author DJ2-PC
 */
public class BuscarElector extends javax.swing.JDialog {

    private Elector elector;
    private ArrayList<Elector> electores;
    AdministrarPartidoElectoral ap = null;
    AdministrarElecciones ae = null;

    /** Creates new form BuscarElector */
    public BuscarElector(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.elector = new Elector();
        this.electores = (ArrayList<Elector>) new ConsultasBD().buscarTodos("Elector");
        initComponents();
        jBtnImportarElector.setEnabled(false);
    }

    public BuscarElector(java.awt.Frame parent, boolean modal, AdministrarPartidoElectoral ap) {
        super(parent, modal);
        this.elector = new Elector();
        this.ap = ap;
        this.electores = (ArrayList<Elector>) new ConsultasBD().buscarTodos("Elector");
        initComponents();
        jBtnImportarElector.setEnabled(true);
    }

    public BuscarElector(java.awt.Frame parent, boolean modal, AdministrarElecciones ae) {
        super(parent, modal);
        this.elector = new Elector();
        this.ae = ae;
        this.electores = (ArrayList<Elector>) new ConsultasBD().buscarTodos("Elector");
        initComponents();
        jBtnImportarElector.setEnabled(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        imagePanel1 = new com.mycsistemas.swingec.panel.ImagePanel();
        curvesPanel1 = new com.mycsistemas.swingec.panel.CurvesPanel();
        jLblBuscarElector = new javax.swing.JLabel();
        jLblCritBus = new javax.swing.JLabel();
        jCbxBuscarElector = new javax.swing.JComboBox();
        jTxtBuscarElector = new javax.swing.JTextField();
        jBtnBuscarElector = new javax.swing.JButton();
        jPanelElectores = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTblElectores = new javax.swing.JTable();
        jBtnImportarElector = new javax.swing.JButton();
        jBtnSalirElector = new javax.swing.JButton();
        jBtnSalirElector1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Buscador de Electores");

        imagePanel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/''_10102012_1738.jpeg"))); // NOI18N
        imagePanel1.setLayout(null);

        curvesPanel1.setLayout(null);

        jLblBuscarElector.setFont(new java.awt.Font("Georgia", 3, 18));
        jLblBuscarElector.setForeground(new java.awt.Color(255, 255, 255));
        jLblBuscarElector.setText("Buscador de Electores");
        curvesPanel1.add(jLblBuscarElector);
        jLblBuscarElector.setBounds(230, 30, 260, 21);

        jLblCritBus.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblCritBus.setForeground(new java.awt.Color(255, 255, 255));
        jLblCritBus.setText("Criterio de Busqueda:");
        curvesPanel1.add(jLblCritBus);
        jLblCritBus.setBounds(40, 60, 160, 30);

        jCbxBuscarElector.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cédula", "Nombres", "Apellidos", "Todos los Electores" }));
        jCbxBuscarElector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCbxBuscarElectorActionPerformed(evt);
            }
        });
        curvesPanel1.add(jCbxBuscarElector);
        jCbxBuscarElector.setBounds(30, 100, 190, 30);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${elec.cedula}"), jTxtBuscarElector, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jTxtBuscarElector.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTxtBuscarElectorKeyPressed(evt);
            }
        });
        curvesPanel1.add(jTxtBuscarElector);
        jTxtBuscarElector.setBounds(230, 100, 350, 30);

        jBtnBuscarElector.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/search_32.png"))); // NOI18N
        jBtnBuscarElector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnBuscarElectorActionPerformed(evt);
            }
        });
        curvesPanel1.add(jBtnBuscarElector);
        jBtnBuscarElector.setBounds(590, 100, 40, 30);

        jPanelElectores.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Electores", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 10), java.awt.Color.white)); // NOI18N
        jPanelElectores.setForeground(new java.awt.Color(255, 255, 255));
        jPanelElectores.setOpaque(false);
        jPanelElectores.setLayout(null);

        jTblElectores.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${electores}");
        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, jTblElectores);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cedula}"));
        columnBinding.setColumnName("Cedula");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nombre}"));
        columnBinding.setColumnName("Nombre");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${apellido}"));
        columnBinding.setColumnName("Apellido");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${anioparalelo}"));
        columnBinding.setColumnName("Anioparalelo");
        columnBinding.setColumnClass(sisvoto.modelo.Anio_Paralelo.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(jTblElectores);

        jPanelElectores.add(jScrollPane1);
        jScrollPane1.setBounds(10, 20, 580, 140);

        curvesPanel1.add(jPanelElectores);
        jPanelElectores.setBounds(30, 140, 610, 170);

        jBtnImportarElector.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/right_32.png"))); // NOI18N
        jBtnImportarElector.setText("Importar");
        jBtnImportarElector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnImportarElectorActionPerformed(evt);
            }
        });
        curvesPanel1.add(jBtnImportarElector);
        jBtnImportarElector.setBounds(140, 320, 120, 41);

        jBtnSalirElector.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/DeleteRed.png"))); // NOI18N
        jBtnSalirElector.setText("Salir");
        jBtnSalirElector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSalirElectorActionPerformed(evt);
            }
        });
        curvesPanel1.add(jBtnSalirElector);
        jBtnSalirElector.setBounds(420, 320, 120, 41);

        jBtnSalirElector1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/print_32.png"))); // NOI18N
        jBtnSalirElector1.setText("Imprimir");
        jBtnSalirElector1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSalirElector1ActionPerformed(evt);
            }
        });
        curvesPanel1.add(jBtnSalirElector1);
        jBtnSalirElector1.setBounds(280, 320, 120, 41);

        imagePanel1.add(curvesPanel1);
        curvesPanel1.setBounds(0, 0, 660, 380);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 666, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE)
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jBtnSalirElectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSalirElectorActionPerformed
// TODO add your handling code here:
    dispose();
}//GEN-LAST:event_jBtnSalirElectorActionPerformed

private void jCbxBuscarElectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCbxBuscarElectorActionPerformed
// TODO add your handling code here:
    if (jCbxBuscarElector.getSelectedItem().toString().equals("Todos los Electores")) {
        jTxtBuscarElector.setEnabled(false);
        jBtnBuscarElector.requestFocus();
        jBtnBuscarElectorActionPerformed(null);
    } else {
        jTxtBuscarElector.setEnabled(true);
    }
    jTxtBuscarElector.setText("");
}//GEN-LAST:event_jCbxBuscarElectorActionPerformed

private void jBtnBuscarElectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnBuscarElectorActionPerformed
// TODO add your handling code here:
    if (jCbxBuscarElector.getSelectedItem().toString().equals("Todos los Electores")) {
        bindingGroup.unbind();
        this.electores = (ArrayList<Elector>) new ConsultasBD().buscarTodos("Elector");
        jTblElectores.updateUI();
        bindingGroup.bind();
        return;
    }
    if (jCbxBuscarElector.getSelectedItem().toString().equals("Cédula")) {
        if (new Util().esCedulaValida(jTxtBuscarElector.getText())) {
            bindingGroup.unbind();
            this.electores = (ArrayList<Elector>) new ConsultasBD().buscarPOR("Elector", "cedula", "'" + jTxtBuscarElector.getText() + "'");
            jTblElectores.updateUI();
            bindingGroup.bind();
        } else {
            new Mensajes().StandarWarning(this, "Ingrese una cédula válida");
        }
        return;
    }
    if (jCbxBuscarElector.getSelectedItem().toString().equals("Nombres")) {
        bindingGroup.unbind();
        this.electores = (ArrayList<Elector>) new ConsultasBD().buscarLIKE("Elector", "nombre", jTxtBuscarElector.getText());
        jTblElectores.updateUI();
        bindingGroup.bind();
        return;
    }
    if (jCbxBuscarElector.getSelectedItem().toString().equals("Apellidos")) {
        bindingGroup.unbind();
        this.electores = (ArrayList<Elector>) new ConsultasBD().buscarLIKE("Elector", "apellido", jTxtBuscarElector.getText());
        jTblElectores.updateUI();
        bindingGroup.bind();
        return;
    }
}//GEN-LAST:event_jBtnBuscarElectorActionPerformed

private void jTxtBuscarElectorKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTxtBuscarElectorKeyPressed
// TODO add your handling code here:
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        jBtnBuscarElector.requestFocus();
        jBtnBuscarElectorActionPerformed(null);
    }
    jTxtBuscarElector.requestFocus();
}//GEN-LAST:event_jTxtBuscarElectorKeyPressed

private void jBtnImportarElectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnImportarElectorActionPerformed
// TODO add your handling code here:
    if (jTblElectores.getSelectedRow() != -1) {
        String idpriv = (String) jTblElectores.getValueAt(jTblElectores.getSelectedRow(), 0);
        ArrayList<Elector> paux = (ArrayList<Elector>) new ConsultasBD().buscarPOR("Elector", "cedula", "'" + idpriv + "'");
        this.elector = paux.get(0);
        if (ap != null) {
            ap.setElector(elector);
        }
        if (ae != null) {
            ae.electores.add(elector);
        }
        this.dispose();
    }
}//GEN-LAST:event_jBtnImportarElectorActionPerformed

private void jBtnSalirElector1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSalirElector1ActionPerformed
    try {
        this.dispose();
        new Reportes().reporte("todoselectores", this,null);
    } catch (Exception e) {
        System.out.println(e);
    }


}//GEN-LAST:event_jBtnSalirElector1ActionPerformed

    public Elector getElector() {
        return elector;
    }

    public void setElector(Elector elector) {
        this.elector = elector;
    }

    public ArrayList<Elector> getElectores() {
        return electores;
    }

    public void setElectores(ArrayList<Elector> electores) {
        this.electores = electores;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.mycsistemas.swingec.panel.CurvesPanel curvesPanel1;
    private com.mycsistemas.swingec.panel.ImagePanel imagePanel1;
    private javax.swing.JButton jBtnBuscarElector;
    private javax.swing.JButton jBtnImportarElector;
    private javax.swing.JButton jBtnSalirElector;
    private javax.swing.JButton jBtnSalirElector1;
    private javax.swing.JComboBox jCbxBuscarElector;
    private javax.swing.JLabel jLblBuscarElector;
    private javax.swing.JLabel jLblCritBus;
    private javax.swing.JPanel jPanelElectores;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTblElectores;
    private javax.swing.JTextField jTxtBuscarElector;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
